﻿Imports System.Data.OleDb
Public Class editProgress
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim UserID As String
        Dim MemUser As MembershipUser
        MemUser = Membership.GetUser
        UserID = MemUser.ProviderUserKey.ToString

        If Not IsPostBack Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            Dim SqlString As String = "select * from tblProgress where ID = @f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", Request.Params("PID"))
            oleDbConn.Open()

            Dim dr As OleDbDataReader = cmd.ExecuteReader()

            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("Title")) Then txtTitle.Text = dr("Title")
                If Not IsDBNull(dr("Description")) Then txtDescription.Text = dr("Description")
                If Not IsDBNull(dr("Picture")) Then imgPicture.ImageUrl = dr("Picture")
                If Not IsDBNull(dr("Weight")) Then txtWeight.Text = dr("Weight")

            End If
        End If
    End Sub

    Protected Sub btnEditProgressUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnEditProgressUpdate.Click
        Dim UserID As String
        Dim MemUser As MembershipUser
        MemUser = Membership.GetUser
        UserID = MemUser.ProviderUserKey.ToString

        Dim newFileName As String = ""
        Dim SqlString As String

        If fuPicture.HasFile Then
            Dim myGUID As New System.Guid
            myGUID = Guid.NewGuid
            newFileName = myGUID.ToString() + ".jpg"
            Dim picLocation = Request.MapPath("pictures") & "/" & newFileName
            fuPicture.SaveAs(picLocation)
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

        If fuPicture.HasFile Then
            SqlString = "Update tblProgress set UserID=@f1,UserName=@f2,Title=@f3,Description=@f4,Picture=@f5,Weight=@f6,EntryDate=@f7 where ID = @f8"
        Else
            SqlString = "Update tblProgress set UserID=@f1,UserName=@f2,Title=@f3,Description=@f4,Weight=@f6,EntryDate=@f7 where ID = @f8"
        End If

        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)

        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", UserID)
        cmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f3", txtTitle.Text)
        cmd.Parameters.AddWithValue("@f4", txtDescription.Text)
        If fuPicture.HasFile Then cmd.Parameters.AddWithValue("@f5", "pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f6", txtWeight.Text)
        cmd.Parameters.AddWithValue("@f7", Date.Today)
        cmd.Parameters.AddWithValue("@f8", Request.Params("PID"))

        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()

        Response.Redirect("EditProgressConfirmation.aspx")
    End Sub
End Class